package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.HospitalDeptDao;
import com.ym.medical.domain.entity.HospitalDeptEntity;
import com.ym.medical.domain.param.HospitalDeptQryParam;
import com.ym.medical.domain.param.HospitalDeptQueryParam;
import com.ym.medical.domain.param.HospitalDeptUpdateParam;
import com.ym.medical.domain.vo.HospitalDeptListPcVO;
import com.ym.medical.domain.vo.HospitalDeptVo;
import com.ym.medical.domain.vo.HospitalDeptVo2;
import com.ym.medical.service.IHospitalDeptSV;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.constant.ErrorConstant;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
* 【请填写功能名称】Service业务层处理
*
* @author ym
* @date 2020-08-20
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class HospitalDeptSVImpl extends BaseSVImpl<HospitalDeptEntity,HospitalDeptDao> implements IHospitalDeptSV {

	/**
	 * 查询【请填写功能名称】列表
	 *
	 * @param queryParam 【请填写功能名称】 查询参数
	 * @return 【请填写功能名称】
	 */
	@Override
	public List<HospitalDeptListPcVO> selectListPC(HospitalDeptQueryParam queryParam) {
		List<HospitalDeptListPcVO> hospitalDepts = mapper.selectListPC(queryParam);
		return hospitalDepts;
	}

	/**
	 * 新增【请填写功能名称】
	 *
	 * @param updateParam 【请填写功能名称】 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(HospitalDeptUpdateParam updateParam) {
            HospitalDeptEntity entity = updateParam.covert();
		this.insert(entity);
		return entity.getId();
	}

	/**
	 * 修改【请填写功能名称】
	 *
	 * @param updateParam 【请填写功能名称】 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(HospitalDeptUpdateParam updateParam) {
            HospitalDeptEntity entity = updateParam.covert("createTime");
		int result = this.updateById(entity);
		return result;
	}

	/**
	 * 医院code 查询对应医院科室
	 *
	 * @param hosCode
	 * @return
	 */
	@Override
	public List<HospitalDeptEntity> selectDept(String hosCode) {
		List<HospitalDeptEntity> hospitalDepts =
				mapper.selectDepts(hosCode);
		return hospitalDepts;
	}

	/**
	 * 科室下拉搜索
	 * @param hosName
	 * @param deptName
	 * @return
	 */
	@Override
	public List<HospitalDeptEntity> searchDept(String hosName, String deptName) {
		List<HospitalDeptEntity> deptList = mapper.searchDept(hosName,deptName);
		return deptList;
	}
	/**
	 * 科室列表
	 *
	 * @param pageNo
	 * @param limit
	 * @param hospitalDeptModel
	 * @return
	 */
	@Override
	public PageInfo<HospitalDeptVo> getHospitalDeptList(int pageNo, int limit, HospitalDeptQryParam hospitalDeptModel) {
		PageHelper.startPage(pageNo, limit);
		List<HospitalDeptVo> hospitalDeptList = mapper.getHospitalDeptList(hospitalDeptModel);
		PageInfo<HospitalDeptVo> hospitalDeptPageInfo = new PageInfo<>(hospitalDeptList);
		return hospitalDeptPageInfo;
	}

	/**
	 * 科室导入
	 *
	 * @param hospitalDeptList
	 * @return
	 */
	@Override
	public Map<String, Object> importHosDept(List<HospitalDeptEntity> hospitalDeptList) {
		List<HospitalDeptEntity> failList = new ArrayList<>();
		List<HospitalDeptEntity> hospitalDepts = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		int rows = 0;
		for (HospitalDeptEntity hospitalDept : hospitalDeptList) {
			if (null == hospitalDept.getDeptName() || "".equals(hospitalDept.getDeptName())
					&& null == hospitalDept.getDeptCode() || "".equals(hospitalDept.getDeptCode())) {
				failList.add(hospitalDept);
				continue;
			}
			hospitalDept.setCreateTime(new Date());
			hospitalDept.setUpdateTime(new Date());
			hospitalDept.setIsShow("0");
			hospitalDept.setState("01");
			try {
				//hospitalDeptList1.add(hospitalDept);
				mapper.insertSelective(hospitalDept);
				rows++;
			} catch (Exception e) {
				log.info("HospitalDepetSVImpl importHosDept", e.getMessage());
				if (e instanceof DuplicateKeyException) {
					failList.add(hospitalDept);
				}
			}
		}
		map.put("failDetail", failList);
		map.put("successCount", rows);
		return map;
	}

	/**
	 * 科室信息列表
	 *
	 * @return
	 */
	@Override
	public PageInfo<HospitalDeptVo2> getAllHospitalDept(int pageNo, int limit, HospitalDeptQryParam model) {
		PageHelper.startPage(pageNo,limit);
		List<HospitalDeptVo2> hospitalDeptList = mapper.getAllHospitalDept(model);
		if (hospitalDeptList.isEmpty()){
			throw new BusinessException("没有您要的数据");
		}
		return new PageInfo<>(hospitalDeptList);
	}
	/**
	 * 科室更改
	 *
	 * @param hospitalDept 科室信息
	 * @return
	 */
	@Override
	public int updateHosDept(HospitalDeptEntity hospitalDept) {
		hospitalDept.setUpdateTime(new Date());
		int rows = mapper.updateByPrimaryKeySelective(hospitalDept);
		return rows;
	}
	/**
	 * 科室新增
	 *
	 * @param hospitalDept 科室信息
	 * @return
	 */
	@Override
	public int addHosDept(HospitalDeptEntity hospitalDept) {
		if (null == hospitalDept) {
			throw new BusinessException("新增内容不能为空");
		}
		if (StringUtils.isEmpty(hospitalDept.getDeptCode())) {
			throw new BusinessException("科室编码不能为空");
		}
		if (StringUtils.isEmpty(hospitalDept.getDeptName())) {
			throw new BusinessException("科室名称不能为空");
		}

		hospitalDept.setCreateTime(new Date());
		hospitalDept.setUpdateTime(new Date());

		int rows = 0;
		try {
			rows = mapper.insertSelective(hospitalDept);
		} catch (Exception e) {
			if (e instanceof DuplicateKeyException) {
				throw new BusinessException("编码重复");
			}
			log.info("HospitalDepetSVImpl addHosDept", e.getMessage());
		}
		return rows;
	}
}
